/**
 * @Description:
 * @Author: CHEHSHUANG
 * @Date: 2019/1/23
 */
import React, {Component, Fragment} from 'react';
import {Checkbox, Form, Input,Select,Modal} from "antd"
import {checkInputCode, getUserInfo} from "../../../commons/utils/CommonUtils";
import TreeSelectWithService from "../../../commons/components/TreeSelectWithService";
import {organizationConfig} from "../../../configs/CommonComponentsConfig";

const FormItem = Form.Item;
const formLayout = {
    labelCol: {span: 8},
    wrapperCol: {span: 15}
}

class EditRoleModal extends Component {
    getFormValue = () => {
        let result = false;
        this.props.form.validateFieldsAndScroll((err, values) => {
            if (!err) {
                result = values;
                result.tenantCode = getUserInfo().tenantCode;
                result.featureRoleGroup = {id: this.props.roleGroupId};
                result.code = result.tenantCode +"-"+result.code;
                const {isEdit,editData} = this.props;
                if (isEdit && editData) {
                    result = {...editData,...result}
                }
            }
        });
        return result;
    }
    componentDidMount(){
        this.props.onRef && this.props.onRef(this);
    }
    render() {
        const {title, visible, onOk, onCancel,isEdit, editData,confirmLoading} = this.props;
        const {getFieldDecorator} = this.props.form;
        const {tenantCode=""} = getUserInfo();
        const maxGroupCodeLen = 20 - tenantCode.length - 1;
        const isPublicRole = this.props.form.getFieldValue("isPublicRole");
        let initPublicRole = false;
        if(isEdit && editData && (editData.publicUserType || editData.publicOrg)){
            initPublicRole = true;
        }
        return (
            <Modal
                width={550}
                centered
                destroyOnClose={true}
                maskClosable={false}
                title={title}
                confirmLoading={confirmLoading}
                visible={visible}
                onCancel={onCancel}
                onOk={onOk}
            >
                <Form>
                    <FormItem
                        label={"代码"}
                        {...formLayout}
                    >
                        {getFieldDecorator('code', {
                            initialValue: isEdit && editData && editData.code ? editData.code.split("-")[1] : "",
                            rules: [
                                {required: true, message: '请输入代码'},
                                {max: maxGroupCodeLen,message: `输入的最大长度为${maxGroupCodeLen}`},checkInputCode()
                            ]
                        })(
                            <Input addonBefore={`${tenantCode}-`}/>
                        )}
                    </FormItem>
                    <FormItem
                        label={"名称"}
                        {...formLayout}
                    >
                        {getFieldDecorator('name', {
                            initialValue: isEdit && editData && editData.name ? editData.name : "",
                            rules: [{required: true, message: '请填写名称!'}]
                        })(
                            <Input placeholder={"请填写名称"}/>
                        )}
                    </FormItem>
                    <FormItem
                        label={"角色类型"}
                        {...formLayout}
                    >
                        {getFieldDecorator('roleType', {
                            initialValue: isEdit && editData ? editData.roleType : "CanUse",
                            rules: [{required: true, message: '请选择角色类型!'}]
                        })(
                            <Select>
                                <Select.Option value="CanUse">可以使用</Select.Option>
                                <Select.Option value="CanAssign">可以分配</Select.Option>
                            </Select>
                        )}
                    </FormItem>
                    <FormItem
                        label={"公共角色"}
                        {...formLayout}
                    >
                        {getFieldDecorator('isPublicRole', {
                            initialValue: initPublicRole,
                            valuePropName: "checked"
                        })(
                            <Checkbox />
                        )}
                    </FormItem>
                    {
                        (isPublicRole || initPublicRole) && (
                            <Fragment>
                                <FormItem
                                    label={"公共角色组织机构"}
                                    {...formLayout}
                                >
                                    {getFieldDecorator('publicOrg.id', {
                                        initialValue: isEdit && editData && editData.publicOrg ? editData.publicOrg.id : "",
                                    })(
                                        <TreeSelectWithService
                                            config={organizationConfig}
                                            initValue={true}
                                        />
                                    )}
                                </FormItem>
                                <FormItem
                                    label={"公共角色用户类型"}
                                    {...formLayout}
                                >
                                    {getFieldDecorator('publicUserType', {
                                        initialValue: isEdit && editData && editData.publicUserType ? editData.publicUserType : "",
                                    })(
                                        <Select>
                                            <Select.Option value="Employee">员工</Select.Option>
                                            <Select.Option value="Supplier">供应商</Select.Option>
                                            <Select.Option value="Customer">客户</Select.Option>
                                            <Select.Option value="Expert">专家</Select.Option>
                                        </Select>
                                    )}
                                </FormItem>
                            </Fragment>
                        )
                    }

                </Form>
            </Modal>
        );
    }
}

export default Form.create()(EditRoleModal);
